// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store/store'
import validation from './plugins/validation/validation'
import time from './filter/time'
import logPop from './plugins/logPop/logPop'
require('animate.css')
Vue.config.productionTip = false
router.beforeEach((to, from, next) => {
  document.title = to.meta.title || '创投内参'
  if (to.matched.some(record => record.meta.footer)) {
    store.commit('user/updataFooter', true)
  } else {
    store.commit('user/updataFooter', false)
  }
  if (to.meta.auth) {  // 如果要登录
    if (localStorage.getItem('token')) {
        // 此处根据token请求登录信息
      store.dispatch('user/getUserInfo').then((response) => {
        if (response.code === '200') {
          store.commit('user/saveUserInfo', response.data)
        }
        next()
      })
    } else {
      next({ name: 'alerts' })
    }
  } else {  // 如果不需要登录
    if (localStorage.getItem('token')) {
      let status = store.state.user.loginStatus
      if (!status) {
        store.dispatch('user/getUserInfo').then((response) => {
          if (response.code === '200') {
            store.commit('user/saveUserInfo', response.data)
          } else if (response.code === '401') {
            store.commit('user/updataTokenStatus', true)
          }
        })
      }
      next()
    } else {
      next()
    }
  }
})
Vue.use(validation)
Vue.use(time)
Vue.use(logPop)
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App }
})
